package com.pj.wisdomlogistics.mvp.model;


import com.google.gson.Gson;
import com.pj.wisdomlogistics.BuildConfig;
import com.pj.wisdomlogistics.entity.ApplyDetailInfo;
import com.pj.wisdomlogistics.entity.response.BaseResponse;
import com.pj.wisdomlogistics.mvp.frame.MvpModel;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.PostFormBuilder;

import java.io.File;
import java.io.IOException;

import okhttp3.Response;
import rx.Observable;
import rx.Subscriber;


public class ApplyDetailModel extends MvpModel {
    private static final String POST_URL = BuildConfig.SERVER_HOST + "meetingApplySign.do";

    public Observable<BaseResponse> meetingApplySign(
            final int meetingApplyID,
            final String userID,
            final File sign,
            final int state,
            final String comment
    ) {
        return
                Observable.create(new Observable.OnSubscribe<BaseResponse>() {
                    @Override
                    public void call(Subscriber<? super BaseResponse> subscriber) {
                        PostFormBuilder formBuilder = OkHttpUtils.post().url(POST_URL);

                        formBuilder.addParams("meetingApplyID", meetingApplyID + "");
                        formBuilder.addParams("userID", userID);

                        if (sign != null) {
                            formBuilder.addFile("sign", sign.getAbsolutePath(), sign);
                        }

                        formBuilder.addParams("state", state + "");
                        formBuilder.addParams("comment", comment);


                        try {
                            Response response = formBuilder.build().execute();
                            String str = response.body().string();
                            BaseResponse postResponse = new Gson().fromJson(str, BaseResponse.class);
                            subscriber.onNext(postResponse);
                            subscriber.onCompleted();
                        } catch (IOException e) {
                            e.printStackTrace();
                            subscriber.onError(e);
                        }
                    }
                });
    }


    public Observable<BaseResponse<ApplyDetailInfo>> findMeetingApplyDetail(int meetingApplyID) {
        return mApiService.findMeetingApplyDetail(meetingApplyID);
    }

    public Observable<BaseResponse> meetingApplyReview(
            int meetingApplyID,
            String userID,
            String comment,
            int state
    ) {
        return mApiService.meetingApplyReview(
                meetingApplyID,
                userID,
                comment,
                state
        );
    }

    public Observable<BaseResponse> meetingApplyAllot(int meetingApplyID, String userID,
                                                      String comment, String serverIDs, int state) {
        return mApiService.meetingApplyAllot(meetingApplyID, userID, comment, serverIDs, state);
    }

    public Observable<BaseResponse> meetingApplyKZ(
            String userID,
            String meetingApplyID,
            String state,
            String comment
    ) {
        return mApiService.meetingApplyKZ(userID, meetingApplyID, state, comment);
    }

    public Observable<BaseResponse> cancelMeetingApply(
            int meetingApplyID
    ) {
        return mApiService.cancelMeetingApply(meetingApplyID);
    }
}
